/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.ui.Widget;

/**
 * Detects whether the value of one widget equals to the other.
 * 
 * @author Dop Sun Jul 17, 2008
 */
public class EqualRule implements ValidationRule {

	/**
	 * @param target
	 * @return the validation rule for string widget
	 */
	public static final EqualRule equalString(Widget target) {
		return new EqualRule(WidgetReaders.getStringReader(), target);
	}

	/**
	 * @param target
	 * @return the validation rule for integer widget
	 */
	public static final EqualRule equalInteger(Widget target) {
		return new EqualRule(WidgetReaders.getIntegerReader(), target);
	}
	/**
	 * @param target
	 * @return the validation rule for long widget
	 */
	public static final EqualRule equalLong(Widget target) {
		return new EqualRule(WidgetReaders.getLongReader(), target);
	}
	/**
	 * @param target
	 * @return the validation rule for float widget
	 */
	public static final EqualRule equalFloat(Widget target) {
		return new EqualRule(WidgetReaders.getFloatReader(), target);
	}
	/**
	 * @param target
	 * @return the validation rule for double widget
	 */
	public static final EqualRule equalDouble(Widget target) {
		return new EqualRule(WidgetReaders.getDoubleReader(), target);
	}
	/**
	 * @param target
	 * @return the validation rule for string widget
	 */
	public static final EqualRule equalBoolean(Widget target) {
		return new EqualRule(WidgetReaders.getBooleanReader(), target);
	}
	
	private WidgetReader<?> mWidgetReader;

	private Widget mTarget;

	/**
	 * Initializes a new instance of <code>EqualRule</code>.
	 * 
	 * @param widgetReader
	 * @param target
	 */
	public EqualRule(WidgetReader<?> widgetReader, Widget target) {
		mWidgetReader = widgetReader;
		mTarget = target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.softao.ming.gwt.user.client.ValidationRule#execute(com.google.gwt
	 * .user.client.ui.Widget)
	 */
	public boolean execute(Widget widget) {
		if (this.mTarget == null || this.mWidgetReader == null) {
			return true;
		}

		if (widget == null) {
			return false;
		}

		Object targetValue = this.mWidgetReader.read(mTarget);
		if (targetValue == null) {
			return true;
		}
		return targetValue.equals(mWidgetReader.read(widget));
	}

	/**
	 * @param widgetReader
	 *            the widgetReader to set
	 */
	public void setWidgetReader(WidgetReader<?> widgetReader) {
		mWidgetReader = widgetReader;
	}

	/**
	 * @return the widgetReader
	 */
	public WidgetReader<?> getWidgetReader() {
		return mWidgetReader;
	}

	/**
	 * @param target
	 *            the target to set
	 */
	public void setTarget(Widget target) {
		mTarget = target;
	}

	/**
	 * @return the target
	 */
	public Widget getTarget() {
		return mTarget;
	}

}
